﻿<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Addmin/BackEnd/MTP_Admin.Master" CodeBehind="Product_Detail.aspx.cs"
    Inherits="Bussiness.View.BackEnd.Product_Detail" %>
   
<%@ Register Src="~/Addmin/BackEnd/User_Control/Image_Product.ascx" TagName="LovImage"
    TagPrefix="uc1" %>
    
  <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
                        <ext:Panel ID="Panel2" runat="server" Title="Chi tiết sản phẩm" PaddingSummary="10px 5px 0 5px" Layout="ColumnLayout" Height="500">
                            <Items>
                                <ext:Container ID="Container2" runat="server" LabelAlign="Left" Layout="FormLayout"
                                    ColumnWidth=".45">
                                    <Items>
                                        <ext:Hidden runat="server" ID="pu_id" IDMode="Static" />
                                        <ext:TextField runat="server" ID="pu_maHang" IDMode="Static" FieldLabel="Mã hàng"
                                            AnchorHorizontal="100%" MaxLength="20" AllowBlank="false"/>
                                        <ext:TextField runat="server" ID="pu_tenHang" IDMode="Static" FieldLabel="Tên hàng hóa"
                                            AnchorHorizontal="100%" MaxLength="50" AllowBlank="false"/>
                                        <ext:SelectBox ID="pu_LoaiSP_Grid" runat="server" FieldLabel="Danh mục cha" AnchorHorizontal="100%"
                                            DataIndex="Status" IDMode="Static" ValueField="ID" DisplayField="Name" AllowBlank="false">
                                            <Store>
                                                <ext:Store ID="pu_LoaiSP_Store" runat="server">
                                                    <Reader>
                                                        <ext:ArrayReader IDProperty="ID">
                                                            <Fields>
                                                                <ext:RecordField Name="ID" Type="String" Mapping="ID" />
                                                                <ext:RecordField Name="Name" Type="String" Mapping="Name" />
                                                                <ext:RecordField Name="IsSize" Type="Boolean" Mapping="IsSize" />
                                                            </Fields>
                                                        </ext:ArrayReader>
                                                    </Reader>
                                                </ext:Store>
                                            </Store>
                                            <Listeners>
                                                <BeforeSelect Fn="SelectParentCatelogies" />
                                            </Listeners>
                                            <DirectEvents>
                                                <Select OnEvent="SelectParent"></Select>
                                            </DirectEvents>
                                        </ext:SelectBox>
                                        <ext:SelectBox ID="pu_DMCon_Grid" runat="server" FieldLabel="Danh mục con" AnchorHorizontal="100%"
                                            DataIndex="Status" IDMode="Static" ValueField="ID" DisplayField="Name" AllowBlank="false" >
                                            <Store>
                                                <ext:Store ID="pu_DMCon_Store" runat="server">
                                                    <Reader>
                                                        <ext:ArrayReader IDProperty="ID">
                                                            <Fields>
                                                                <ext:RecordField Name="ID" Type="String" Mapping="ID"  />
                                                                <ext:RecordField Name="Name" Type="String" Mapping="Name" />
                                                            </Fields>
                                                        </ext:ArrayReader>
                                                    </Reader>
                                                </ext:Store>
                                            </Store>
                                        </ext:SelectBox>
                                        
                                       <ext:GridPanel ID="pu_sizeSL_Grid" IDMode="Static" runat="server" Height="150" AutoScroll="true"
                                            Region="Center" ColumnWidth=".6" FieldLabel="Size-Số lượng">
                                            <Store>
                                                <ext:Store ID="pu_sizeSL_Store" runat="server" AutoDataBind="true" SkipIdForNewRecords="false">
                                                    <Reader>
                                                        <ext:JsonReader>
                                                            <Fields>
                                                                <ext:RecordField Name="Size" Type="String" Mapping="Size" />
                                                                <ext:RecordField Name="Count" Type="String" Mapping="Count" />
                                                            </Fields>
                                                        </ext:JsonReader>
                                                    </Reader>
                                                </ext:Store>
                                            </Store>
                                            <ColumnModel ID="ColumnModel12" runat="server" AutoDataBind="true">
                                                <Columns>
                                                    <ext:RowNumbererColumn />
                                                    <ext:Column Header="Size" DataIndex="Size" AutoDataBind="true" MenuDisabled="True">
                                                        <Editor>
                                                            <ext:TextField ID="pu_size" IDMode="Static" AllowBlank="false" runat="server">
                                                            </ext:TextField>
                                                        </Editor>
                                                    </ext:Column>
                                                    <ext:Column Header="Số lượng" DataIndex="Count" AutoDataBind="true" MenuDisabled="True">
                                                        <Editor>
                                                            <ext:TextField ID="pu_size_amount" IDMode="Static" AllowBlank="false" runat="server">
                                                            </ext:TextField>
                                                        </Editor>
                                                    </ext:Column>
                                                </Columns>
                                            </ColumnModel>
                                            <SelectionModel>
                                                <ext:RowSelectionModel ID="RowSelectionModel12" runat="server" SingleSelect="true">
                                                </ext:RowSelectionModel>
                                            </SelectionModel>
                                            <Plugins>
                                                <ext:RowEditor ID="RowEditor1" SaveText="Lưu" CancelText="Thoát" ErrorSummary="False">
                                                    <Listeners>
                                                        <CancelEdit Handler="DeleteRow()" />
                                                        <AfterEdit Handler="AddSize()" />
                                                    </Listeners>
                                                </ext:RowEditor>
                                            </Plugins>
                                            <Buttons>
                                                <ext:Button ID="pu_Button2" Text="Thêm mới" Icon="Add" runat="server">
                                                    <Listeners>
                                                        <Click Fn="AddProperty" />
                                                    </Listeners>
                                                </ext:Button>
                                                <ext:Button ID="pu_Button3" Text="Xóa" Icon="Delete" runat="server">
                                                    <Listeners>
                                                        <Click Handler="#{pu_sizeSL_Grid}.deleteSelected();" />
                                                    </Listeners>
                                                </ext:Button>
                                            </Buttons>
                                        </ext:GridPanel>
                                    </Items>
                                </ext:Container>
                                <ext:Container ID="Container3" runat="server"  ColumnWidth=".1"/>
                                <ext:Container ID="Container1" runat="server" LabelAlign="Left" Layout="FormLayout"
                                    ColumnWidth=".45" >
                                    <Items>
                                        <ext:NumberField runat="server" ID="pu_soLuong" IDMode="Static" FieldLabel="Số lượng"
                                            AnchorHorizontal="100%" MaxValue="999999999999999999" MinValue="0" MaxLengthText="18"
                                            MaxLength="18" />
                                        <ext:TextField runat="server" ID="pu_donViTinh" IDMode="Static" FieldLabel="Đơn vị tính"
                                            AnchorHorizontal="100%" MaxLength="20"/>
                                        <ext:DateField runat="server" ID="pu_ngayNhap" IDMode="Static" FieldLabel="Ngày nhập"
                                            AnchorHorizontal="100%" />
                                        
                                        <ext:CompositeField ID="CompositeField1" runat="server">
                                            <Items>
                                                <ext:NumberField runat="server" ID="pu_giaNhap" IDMode="Static" FieldLabel="Giá nhập"
                                                    AnchorHorizontal="100%" MaxValue="999999999999999999" MinValue="0" MaxLengthText="18"
                                                    MaxLength="18" />
                                                <ext:Label ID="Label1" runat="server" Text="VND">
                                                </ext:Label>
                                            </Items>
                                        </ext:CompositeField>
                                        <ext:CompositeField ID="CompositeField2" runat="server">
                                            <Items>
                                                <ext:NumberField runat="server" ID="pu_giaBan" IDMode="Static" FieldLabel="Gía bán"
                                                    AnchorHorizontal="100%" MaxValue="999999999999999999" MinValue="0" MaxLengthText="18"
                                                    MaxLength="18" />
                                                <ext:Label ID="Label2" runat="server" Text="VND">
                                                </ext:Label>
                                            </Items>
                                        </ext:CompositeField>
                                        <ext:TextArea runat="server" ID="pu_moTaSP" IDMode="Static" Width="250" FieldLabel="Mô tả sản phẩm">
                                        </ext:TextArea>
                                       <ext:CompositeField ID="CompositeField3" runat="server">
                                            <Items>
                                                <ext:NumberField runat="server" ID="pu_giamGia" IDMode="Static" FieldLabel="Giảm giá"
                                                    AnchorHorizontal="100%" MaxValue="100" MinValue="0" MaxLengthText="2"
                                                    MaxLength="2" />
                                                <ext:Label ID="Label3" runat="server" Text="%">
                                                </ext:Label>
                                            </Items>
                                        </ext:CompositeField>
                                    </Items>
                                </ext:Container>
                            </Items>
                            <Buttons>
                                <ext:Button Text="Lưu" Icon="Add" runat="server" ID="btSave">
                                    <DirectEvents>
                                        <Click OnEvent="save" Before="validate">
                                            <ExtraParams>
                                                <ext:Parameter Name="valueGrid" Value="Ext.encode(#{pu_sizeSL_Grid}.getRowsValues({selectedOnly : false}))"
                                                    Mode="Raw" />
                                            </ExtraParams>
                                        </Click>
                                    </DirectEvents>
                                </ext:Button>
                                 <ext:Button  Text="Ảnh sản phẩm" Icon="ImageAdd" runat="server" ID="Button1">
                                    <Listeners >
                                        <Click Fn = "show_Image" />
                                    </Listeners>
                                 </ext:Button>
                                
                            </Buttons>
                        </ext:Panel>

         <uc1:LovImage ID="lovImage" runat="server" />

<script type="text/javascript">
    var showResult = function(btn) {
    };
    function show_Image() {
        if (Ext.getCmp('pu_id').getValue() == null || Ext.getCmp('pu_id').getValue() == "") {
            Ext.Msg.alert("Lỗi", "Bạn phải Save thông tin sản phẩm trước.");
            return false;
        }else
        Ext.getCmp('mg_Image').setVisible(true);
    }

    function validate() {
        if (Ext.getCmp('pu_LoaiSP_Grid').getValue() == "" || Ext.getCmp('pu_LoaiSP_Grid').getValue() == null) {
            Ext.Msg.alert("Lỗi","Bạn phải chọn danh mục cha"); return false;
        }
        if (Ext.getCmp('pu_DMCon_Grid').getValue() == "" || Ext.getCmp('pu_DMCon_Grid').getValue() == null) {
            Ext.Msg.alert("Lỗi","Bạn phải chọn danh mục con");
            return false;
        }
        
        if (Ext.getCmp('pu_ngayNhap').getValue() == "" || Ext.getCmp('pu_ngayNhap').getValue() == null) {
            Ext.Msg.alert("Lỗi","Bạn phải chọn Ngày nhập");
            return false;
        }
        
        
//            var code = Ext.getCmp('pu_LoaiSP_Grid').getValue();
//            var record =  Ext.getCmp('pu_LoaiSP_Grid').findRecord("ID",code); 
//            if(record.get("IsSize") == true){
//             if (Ext.getCmp('pu_size').getValue() == "" || Ext.getCmp('pu_size').getValue() == null) {
//                Ext.Msg.alert("Lỗi", "Bạn phải nhập Size");
//            return false;
//        }
//        
//        if (Ext.getCmp('pu_size_amount').getValue() == "" || Ext.getCmp('pu_size_amount').getValue() == null) {
//            Ext.Msg.alert("Lỗi", "Bạn phải nhập số lượng Size");
//            return false;
//        }
 //           }

        if (Ext.getCmp('pu_soLuong').getValue() == "" || Ext.getCmp('pu_soLuong').getValue() == null) {
            Ext.Msg.alert("Lỗi", "Bạn phải nhập số lượng sản phẩm");
            return false;
        }
        
        if (Ext.getCmp('pu_maHang').getValue() == "" || Ext.getCmp('pu_maHang').getValue() == null) {
            Ext.Msg.alert("Lỗi", "Bạn phải nhập mã sản phẩm");
            return false;
        }
        if (Ext.getCmp('pu_tenHang').getValue() == "" || Ext.getCmp('pu_tenHang').getValue() == null) {
            Ext.Msg.alert("Lỗi", "Bạn phải nhập Tên sản phẩm");
            return false;
        }
        
        
            
    }
    function AddProperty() {
        var grid = Ext.getCmp('pu_sizeSL_Grid');
        grid.getRowEditor().stopEditing();
        var numOfRow = grid.getStore().getCount();
        grid.insertRecord(numOfRow);
        grid.getView().refresh();
        grid.getView().focusRow(numOfRow);
        grid.getSelectionModel().selectRow(numOfRow);
        grid.getRowEditor().startEditing(numOfRow);
    }

    //tu dong xoa dung khi du lieu tren Grid Null
    var DeleteRow = function() {
        var store = Ext.getCmp('pu_sizeSL_Grid').getStore();
        var index = 0;
        store.each(function(record) {
        if (record.data['Size'] == "" || record.data['Count'] == "") {

                store.removeAt(index);
            }
            index = index + 1;
        }
)
    };


    function AddSize() {
        var store = Ext.getCmp('pu_sizeSL_Grid').getStore();
        var tongSP = 0;
        store.each(function(record) { tongSP += Number(record.data['Count']) });
        Ext.getCmp('pu_soLuong').setValue(Number(tongSP));
    }
    
    function SelectParentCatelogies(){
        Ext.getCmp('pu_DMCon_Grid').clearValue();
    }

</script>
</asp:Content>

